

//? trim只能去掉字符串两头的空格 返回新字符串
function myTrim(str) {
    let newStr = Object.assign(str)
    let left = 0
    let right = newStr.length - 1
    for (let i = 0; i < newStr.length; i ++) {
        if (str[i] !== ' ') {
            left = i
            break
        }
    }
    for (let i = newStr.length - 1; i >= 0; i --) {
        if (str[i] !== ' ') {
            right = i
            break
        }
    }
    return newStr.slice(left,right + 1)
}
//? 正则方法
function myTrim2(str) {
    return str.replace(/^\s+|\s+$/g, '')
}
const str = '  213123  1 '
console.log(myTrim(str))
console.log(myTrim2(str))
